<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
    <script src="./libs/http.js"></script>
    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody class='tbody'>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">

                <!-- 分页代码 -->
                <ul id="pagination-demo" class="pagination-sm"></ul>

            </div>
        </div>
    </div>



    <!-- 单挑新闻信息的模板引擎 -->
    <script id="data_news" type="text/html">
        {{each data.data v}}
        <tr>
            <td>{{v.title}}</td>
            <td>{{v.author}}</td>
            <td>{{v.category}}</td>
            <td class="text-center">{{v.date}}</td>
            <td class="text-center">{{v.state}}</td>
            <td class="text-center">
                <a href="article_edit.html?id={{v.id}}" class="btn btn-default btn-xs btn_edit"
                    data_id="{{v.id}}">编辑</a>
                <a href="javascript:void(0);" class="btn btn-danger btn-xs btn_delete" data_id="{{v.id}}">删除</a>
            </td>
        </tr>
        {{/each}}
    </script>

    <!-- 下拉列表的模板引擎 -->
    <script id="data_list" type="text/html">
        <option value="">所有分类</option>
        {{each data v }}
        <option value="{{v.id}}">{{v.name}}</option>
        {{/each}}
    </script>


    <script>
        //1.页面一加载，ajax请求分类列表
        //2.页面一加载，ajax请求文章列表
        //3.点击筛选按钮 ： form表单中的按钮需要阻止默认跳转
        //4.分页功能
        //5.删除文章
        $(function () {
            //1.页面一加载，ajax请求分类列表
            $.ajax({
                url: bigNew.category_list,
                type: 'get',
                dataType: 'json',
                success: function (backData) {
                    console.log(backData);
                    $('#selCategory').html(template('data_list', backData))
                }
            });
            //2.页面一加载，ajax请求文章列表
            $('#btnSearch').click(function (e) {
                e.preventDefault()
                $.ajax({
                    url: bigNew.article_query,
                    type: 'get',
                    dataType: 'json',
                    data: {
                        type: $('#selCategory').val(),
                        state: $('#selStatus').val(),
                        page: 1,
                        perpage: 10
                    },
                    success: function (backData) {
                        console.log(backData);
                        // 渲染页面
                        $('.tbody').html(template('data_news', backData))
                        //渲染前先删除上一个分页
                        $('#pagination-demo').twbsPagination('destroy');
                        $('#pagination-demo').twbsPagination({
                            totalPages: backData.data.totalPage,
                            visiblePages: 7,
                            first: '首页',
                            prev: '上一页',
                            next: '下一页',
                            last: '尾页',
                            loop: true,
                            onPageClick: function (event, page) {
                                $('#page-content').text('Page ' + page);
                                // 打印当前所处页码
                                console.log(page);
                                getpage(page)
                            }
                        });
                    }
                });
            })
            //此为封装好的根据页码发送请求函数
            function getpage(page) {
                $.ajax({
                    url: bigNew.article_query,
                    type: 'get',
                    dataType: 'json',
                    data: {
                        type: $('#selCategory').val(),
                        state: $('#selStatus').val(),
                        page: page,
                        perpage: 10
                    },
                    success: function (backData) {
                        console.log(backData);
                        // 渲染页面
                        $('.tbody').html(template('data_news', backData))
                    }
                })
            }
            $('#btnSearch').click()

            //删除功能
            $('.tbody').on('click', '.btn_delete', function () {
                $.ajax({
                    url: bigNew.article_delete,
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: $(this).attr('data_id')
                    },
                    success: function (backData) {
                        console.log(backData);
                        $('#btnSearch').click()
                        alert('删除成功')
                    }
                });
            })
        })
    </script>
</body>

</html>